using Game.Core;
using Game.Manager;
using Game.XH;
using UnityEngine;

namespace Game.Component.Operation
{
	public class NeedleObject : MonoBehaviour
	{
		public Game7Component component;

		private SpriteRenderer sprite;

		private Transform point;

		private SpriteRenderer pointSprite;

		private float curY;

		private float startY;

		private float targetY;

		private float totalY = 0.2f;

		private bool isTouch;

		private bool isDestory;

		private bool isPlayAnim;

		private float animTimer;

		private int animStage;

		private float percent;

		private void Awake()
		{
			sprite = base.transform.GetComponent<SpriteRenderer>();
			point = base.transform.parent.Find("Point");
			pointSprite = point.Find("Sprite").GetComponent<SpriteRenderer>();
			curY = base.transform.localPosition.y;
			startY = curY;
			targetY = startY + totalY;
		}

		private void Update()
		{
			if (!isTouch && !isDestory && curY != startY)
			{
				curY -= Time.deltaTime * 2f;
				if (curY <= startY)
				{
					curY = startY;
				}
				SetY(curY);
			}
			if (!isPlayAnim)
			{
				return;
			}
			if (animStage == 0)
			{
				animTimer += Time.deltaTime * 8f;
				point.SetLocalScaleY(Mathf.Lerp(3f, 1f, animTimer));
				if (animTimer >= 1f)
				{
					animTimer = 0f;
					animStage++;
				}
				return;
			}
			animTimer += Time.deltaTime;
			sprite.color = new Color(1f, 1f, 1f, 1f - animTimer);
			pointSprite.color = new Color(1f, 1f, 1f, 1f - animTimer);
			if (animTimer >= 1f)
			{
				isPlayAnim = false;
				base.transform.gameObject.SetActive(false);
				component.Complete(point.position);
			}
		}

		public void IsTouch(bool val)
		{
			isTouch = val;
		}

		public void AddProgress(float valX, float valY)
		{
			if (isTouch)
			{
				base.transform.SetXY(base.transform.position.x + valX, base.transform.position.y + valY);
				curY = base.transform.localPosition.y;
				if (curY >= targetY)
				{
					curY = targetY;
					isTouch = false;
					PlayDestoryAnim();
					Singleton<AudioManager>.Instance.PlaySound("sound_embed");
				}
				else if (curY <= startY)
				{
					curY = startY;
				}
				SetY(curY);
			}
		}

		private void SetY(float val)
		{
			curY = val;
			base.transform.SetLocalXY(0f, curY);
			percent = (curY - startY) / totalY;
			point.SetLocalScaleY(1f + 2f * percent);
		}

		public void PlayDestoryAnim()
		{
			if (!isDestory)
			{
				isDestory = true;
				isPlayAnim = true;
				animStage = 0;
				animTimer = 0f;
				Object.Destroy(base.gameObject.GetComponent<BoxCollider2D>());
			}
		}
	}
}
